/**
 * Created by Administrator on 2016/1/26.
 */
$(function(){
    var W = $(window).width(),
        H = $(window).height(),
        $school_form = $(".school-form"),       //学校表单
        $school_name = $(".school-name"),       //学校名称
        $course_name = $(".course-name"),       //课程名字
        sch_html = $(".school-html"),
        $mask = $(".mask");     //遮罩
    //给浮动层高度
    $mask.height(H);
    $school_form.css("height",H);
    $course_name.css("min-height",H);
    //遮罩隐藏
    $mask.on("click",function(){
        $(this).hide();
        $("body").css("overflow","visible");
    });
    //浮层隐藏
    $school_form.on("click",function(){
        $(this).hide();
        $mask.hide();
    });
    //学校层显示
    $(".button-school").on("click",function(){
        $school_form.show();
        $school_name.show();
        $course_name.hide();
        $("body").css("overflow","hidden");
        sch_html.empty();
    });
    //学校层禁止冒泡
    $school_name.on("click",function(e){
        e.stopPropagation();
    });
    //课程层禁止冒泡
    $course_name.on("click",function(e){
        e.stopPropagation();
    });
    //选择学校里的按钮
    $(".confirm-button").on("click",function(){
        var $more_len = $school_name.find('.more:checked').length;
        var sib_p = $(this).siblings();
        if($more_len>0){
            sib_p.remove();
            $mask.show();
            $school_name.hide();
            $course_name.show();
            $course_name.find("table").each(function(){
                var $display = $(this).css("display");
                if($display === "table"){
                    var $school_html =$(this).attr("data-school"); //获取学校的值
                    sch_html.append("<div class='school-course' data-school="+$school_html+">您选的学校：" + $school_html + "<br /></div>");
                }
            });
        }else {
            if(sib_p){
                sib_p.remove();
                $(this).parent().append("<p style='margin-top: 10px; color: red;'>请选择学校</p>")
            }else {
                $(this).parent().append("<p style='margin-top: 10px; color: red;'>请选择学校</p>")
            }


        }
    });
    //判断选择学校里的input
    $school_name.find('.more').on("click",function(){
        var slft = $(this),
            sch_data = slft.parent(".school").attr("data-school"), //获取学校的值
            checked = slft.prop("checked");
        if(checked){ //如果选中
            $course_name.find("table").each(function(){
                var course_data = $(this).attr("data-school"); //获取学校的值
                if(course_data===sch_data) { //如果选择的学校与课程里的学校相等
                    $(this).show();
                }
            });
        }else { //如果没选中
            $course_name.find("table").each(function(){
                var course_data = $(this).attr("data-school");
                if(course_data===sch_data){ //如果选择的学校与课程里的学校相等
                    $(this).hide();
                    $(this).find("input:checked").prop("checked","");
                }
            });
        }
    });
    //选择课程里的按钮
    //$(".course-button").on("click",function() {
    //    var data={};
    //    $course_name.find("table input:checked").each(function() {
    //        var $this = $(this),
    //            course_html = $this.attr("data-course"), //获取课程名称
    //            school_html = $this.attr("data-school"); //获取学校名称
    //        var school=data[school_html]?data[school_html]:[];
    //        school.push(course_html);
    //        data[school_html]=school;
    //    });
    //    var school_count=0;
    //    for(var i in data ){
    //        school_count++;
    //        //console.log(data[i].length);
    //        if(data[i].length>3){
    //            alert('每个学校最多选三门课');
    //        }
    //        if(data[i].length = null){
    //            alert('你选择了学校'+ i +'，但是没有选择课程');
    //        }
    //
    //    }
    //    if(school_count<1 || school_count>2){
    //        alert('学校个数不正确');
    //    }
    //    console.log(template('tmpl_shoool',data));
    //    $("body").append(
    //        template('tmpl_shoool',data)
    //    );
    //
    //
    //});


    $(".course-button").on("click",function(){
        var select_class=[],
            select_school = [],
            course_p =$(".school-course").find("p"),
            sib_p = $(this).siblings();
        $course_name.find("table input:checked").each(function(){
            var $this = $(this),
                $course_html = $this.attr("data-course"), //获取课程名称
                $school_html = $this.attr("data-school"); //获取学校名称
            select_class.push($course_html);
            select_school.push($school_html);
        });

        if(select_class.length === 0){
            if(sib_p){
                sib_p.remove();
                $(this).parent().append("<p style='margin-top: 10px;color: red'>请选择课程</p>")
            }else {
                $(this).parent().append("<p style='margin-top: 10px;color: red'>请选择课程</p>")
            }
        }
        if(select_class.length>6){
            if(sib_p){
                sib_p.remove();
                $(this).parent().append("<p style='margin-top: 10px;color: red'>每个学校最多选择三门课程</p>")
            }else {
                $(this).parent().append("<p style='margin-top: 10px;color: red'>每个学校最多选择三门课程</p>")
            }
        }
        if(0<select_class.length && select_class.length<=6){

            //获取页面上学校
            sib_p.remove();
            var $sch_data1 = sch_html.find(".school-course").eq(0).data("school");
            var $sch_data2 = sch_html.find(".school-course").eq(1).data("school");
            for(k=0;k<select_class.length;k++){
                var xd_data1 = select_school[k] === $sch_data1;
                if(xd_data1){
                    if(course_p){
                        course_p.remove();
                        sch_html.find(".school-course").eq(0).append("<p>" + select_class[k] + "</p>");
                    }else {
                        sch_html.find(".school-course").eq(0).append("<p>" + select_class[k] + "</p>");
                    }
                }
                if(select_school[k] === $sch_data2){
                    sch_html.find(".school-course").eq(1).append("<p>" + select_class[k] + "</p>");
                }
            }
            var course_len1 = sch_html.find(".school-course").eq(0).find("p").length;
            var course_len2 = sch_html.find(".school-course").eq(1).find("p").length;
            if(course_len1>3 || course_len2>3){
                course_p.remove();
                $(this).parent().append("<p style='margin-top: 10px;color: red'>"+$sch_data1+"或"+$sch_data2+"的课程选多了"+"</p>")
            }else if(course_len1 ==0 || course_len2 ==0){
                course_p.remove();
                $(this).parent().append("<p style='margin-top: 10px;color: red'>"+$sch_data1+"或"+$sch_data2+"中有个学校没有选择课程"+"</p>")
            }else if(0<course_len1<4 || 0<course_len2<4){
                $course_name.hide();
                $mask.hide();
                $school_form.hide();
                $("body").css("overflow","visible");
            }

        }
    });


    //选择住校判断
    $(".last-span input").on("click",function(){
        var slft = $(this);
        slft.prop("checked","checked");
        slft.siblings().prop("checked","");
    });
});